
<template>
 <el-form ref="form" :model="data" :rules="rules" label-width="100px">
                    <el-form-item label="关键字" prop="keyword">
                        <el-input v-model.trim="data.keyword" placeholder="请填写关键字" />
                    </el-form-item>
                      <el-form-item label="消息类型">
                      <el-radio-group v-model="data.msg_type">
                        <el-radio :label="1">文本</el-radio>
                        <el-radio :label="2">图片</el-radio>
                        <el-radio :label="3">图文</el-radio>
                        <el-radio :label="4">小程序</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="回复内容" prop="content" v-if="data.msg_type == 1">
                        <el-input  type="textarea" :rows="2" v-model.trim="data.content" placeholder="请填写回复内容" />
                    </el-form-item>
                     <el-form-item label="图片" prop="imgUrl" v-if="data.msg_type == 2">
                        <el-upload
                        :action="$store.state.settings.media_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.imgUrlfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handleimgUrlSuccess"
                        :before-upload="beforeimgUrlUpload"
                        :on-exceed="handleimgUrlExceed"
                        :on-preview="handleimgUrlPreview"
                        :on-remove="handleimgUrlRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                       
                    </el-form-item>
                    <el-form-item label="消息标题" prop="title" v-if="data.msg_type == 3">
                        <el-input v-model.trim="data.title" placeholder="请填写消息标题" />
                    </el-form-item>

                    <el-form-item label="图文链接消息" prop="description" v-if="data.msg_type == 3">
                        <el-input v-model.trim="data.description" placeholder="请填写图文链接消息" />
                    </el-form-item>
                    <el-form-item label="跳转的链接" prop="url" v-if="data.msg_type == 3">
                        <el-input v-model.trim="data.url" placeholder="请填写图文跳转的链接" />
                    </el-form-item>

                    <el-form-item label="图文链接封面" prop="thumb_url" v-if="data.msg_type == 3">
                        <el-upload
                        :action="$store.state.settings.file_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.thumbUrlfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handlethumbUrlSuccess"
                        :before-upload="beforethumbUrlUpload"
                        :on-exceed="handlethumbUrlExceed"
                        :on-preview="handlethumbUrlPreview"
                        :on-remove="handlethumbUrlRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                       
                    </el-form-item>
                   <el-form-item label="消息标题" prop="title" v-if="data.msg_type == 4">
                        <el-input v-model.trim="data.title" placeholder="请填写消息标题" />
                    </el-form-item>
                     <el-form-item label="小程序的页面路径" prop="pagepath" v-if="data.msg_type == 4">
                        <el-input v-model.trim="data.pagepath" placeholder="小程序的页面路径" />
                    </el-form-item>
                     <el-form-item label="消息封面" prop="thumb_media_id" v-if="data.msg_type == 4">
                        <el-upload
                        :action="$store.state.settings.media_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.imgUrlfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handlethumbMediaSuccess"
                        :before-upload="beforeimgUrlUpload"
                        :on-exceed="handleimgUrlExceed"
                        :on-preview="handleimgUrlPreview"
                        :on-remove="handleimgUrlRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                       
                    </el-form-item>
    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {detail,edit} from '@/api/customer'
import { getToken } from '@/utils/auth'



export default {
  name: 'customerCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading:true,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    keyword: [ 
                { required: true, message: "请输入关键字", trigger: "blur" }
            ], 
            content: [ 
                { required: true, message: "请输入回复内容", trigger: "blur" }
            ], 

      },
      data: {
            keyword:'',
            content:'',
            msg_type:1,
            imgUrlfileList:[],
            thumbUrl:'',
            thumbUrlfileList:[],
            imgUrl:'',
            media_id:'',
            thumb_media_id:'',

         }
    }
  },
  created() {
    const id = this.item.id
    this.id = id
    this.fetchData(id)
  },
  watch: {
    item: {
      handler(val, oldVal) {
        if (this.item.dialogVisible === true 
        ) {
          this.id = val.id
          this.fetchData(val.id)
        }
      },
      deep: true
    }
  },
  methods: {
    
    fetchData(id) {
          detail(id).then(response => {
            this.loading = false;
            this.data = response.data;
            if(response.data.msg_type == 2 || response.data.msg_type == 4 ){
                this.data.imgUrlfileList = response.data.media_url ? response.data.media_url.split(',').map(item=>{return{name:item,url:item}}):[];
            }
          }).catch(err => {
            console.log(err)
          })
        },
    submit() {
      const thiz = this
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        edit({
          id: this.data.id,
          keyword:this.data.keyword,
            content:this.data.content,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
